// pages/index/add.js
const { formatDate } = require('../../utils/date')
const { addRemind,getRemindDetail, changeRemind } = require('../../api/index')
import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast';
import Dialog from '../../miniprogram_npm/@vant/weapp/dialog/dialog';
let app =  getApp();
Page({
  data:{
    nvabarData: {
      showCapsule: 1, //是否显示左上角图标   1表示显示    0表示不显示
      title: '新建提醒', //导航栏 中间的标题
      white: false, // 是就显示白的，不是就显示黑的。
      bgColor:'#F3F7FD' // 加个背景 不加就是没有
    },
    // 导航头的高度
    height: app.globalData.height * 2,


    // 药品名称
    mdName:'',
    // 吃药时间
    startTime:'',
    showTimePick:false,
    // 时间选择器设置
    minDate: new Date().getTime(),
    maxDate: new Date(2030, 10, 1).getTime(),
    currentDate: new Date().getTime(),
    // 吃药次数
    number:'3',
    numbers:[1,2,3,4,5,6,7,8,9,10],
    showNumberPick:false,
    // 吃药间隔
    interval:'3',
    showIntervalPick:false,
    intervals:[1,2,3,4,5,6,7,8,9,10],
    // 吃药天数
    day:'7',
    showDayPick:false,
    days:[1,2,3,4,5,6,7,8,9,10,11,12,13],
    // 吃药频率
    frequency:'每天',
    drugSpace:0,
    showFrequencyPick:false,
    frequencys:[0,1,2,3,4,5,6,7,8,9,10],
    // 单位
    unit:'片',
    showUnitPick:false,
    units:['片','克','毫升','毫克'],
    // 剂量
    dose:"",
    // 备忘录
    textarea:'饭后吃',
    // 判断该页面的功能
    isAddPage:true
  },

  onLoad(options){
    let that  = this
    if(options.page == "detailPage"){
      this.setData({isAddPage:false})

      let id = options.id
      getRemindDetail({id})
        .then(res=>{
          console.log(res)
          const { State, info } = res
          if(State){
            that.setData({
              startTime:info.beginTime,
              number:info.drugNumber,
              interval:info.drugSpace,
              day:info.drugDays,
              frequency:info.drugDuring,
              unit:info.drugUnit,
              dose:info.drugDose,
              textarea:info.note,
              mdName:info.drugName
            })
          }
        })
    }
  },

  // 保存按钮,保存后上传信息
  save(){
    this.sendRequest(
      function(data){
        addRemind(data)
        .then(res=>{
          const { State } = res
          if(State){
            Dialog.alert({
              message: '建立成功,点击返回首页查看',
            }).then(() => {
                    // on close
              wx.navigateBack({delta: 1})
            });
          }else{
            Toast.fail('保存失败');
          }
        })
      }
    )
  },
  
  // 修改
  changeDetail(){
    this.sendRequest(
      function(data){
        changeRemind(data)
          .then(res=> {
            const { State } = res
            if(State) {
              Dialog.alert({
                message: '修改成功,点击返回首页查看',
              }).then(() => {
                wx.navigateBack({delta: 1})
              })
            }else{
              Toast.fail('修改失败')
            }
          })
      }
    )
  },

  onchange(e){
    this.setData({textarea:e.detail.value})
  },

  sendRequest(option){
    let that = this.data
    let data = {
      drugName:that.mdName,
      beginTime:that.startTime,
      drugDays:that.day,
      drugDuring:that.interval,
      drugSpace:that.drugSpace+1,
      drugNumber:that.number,
      drugUnit:that.unit,
      drugDose:that.dose,
      note:that.textarea
    }
    let dataArr = Object.values(data)
    let verification = value => value != '';
    // 判断是否有信息为空值
    let isEmpty = dataArr.every(verification);
    if(isEmpty){
      // 不为空则发送请求建立新提醒
      // 发送订阅消息
      wx.requestSubscribeMessage({
        tmplIds: ['F1gKFuNzoBugSTmOvIAPZ30RQSg-M4jGQ-8IxJtZKsk'],
        success (res) {
          // 发送新添信息
          option(data)
        }
      })
    }else{
      // 为空则给用户提醒
      Toast.fail('提醒不能为空哦~');
    }
  },

  /**
   * @param showXXXPick 打开弹出层
   * @param XXXPickConfirm 选择次数并且关闭弹出层
   * @param XXXPickCancel 关闭弹出层
   */
  showTimePick(){
    this.setData({showTimePick:true})
  },
  onClose(){
    this.setData({showTimePick:false})
  },
  timePickConfirm(event){
    let startTime = formatDate(event.detail)
    this.setData({
      startTime,
      showTimePick:false
    });
  },
  timePickCancel(){
    this.setData({showTimePick:false})
  },
  /**
   * 吃药次数相关函数设置
   */
  showNumberPick(){
    this.setData({showNumberPick:true})
  },
  numberPickConfirm(event){
    const { value, index } = event.detail
    this.setData({
      number:value,
      showNumberPick:false
    })
  },
  numberPickCancel(){
    this.setData({showNumberPick:false})
  },
  /**
   * 吃药间隔相关设置
   */
  showIntervalPick(){
    this.setData({showIntervalPick:true})
  },
  intervalPickConfirm(event){
    const { value, index } = event.detail
    this.setData({
      interval:value,
      showIntervalPick:false
    })
  },
  intervalPickCancel(){
    this.setData({showIntervalPick:false})
  },
  // 吃药天数相关函数
  showDayPick(){
    this.setData({showDayPick:true})
  },
  dayPickConfirm(event){
    let { value, index } = event.detail
    this.setData({
      day:value,
      showDayPick:false
    })
  },
  dayPickCancel(){
    this.setData({showDayPick:false})
  },
  // 吃药频率相关函数
  showFrequencyPick(){
    this.setData({showFrequencyPick:true})
  },
  frequencyPickConfirm(event){
    let { value, index } = event.detail
    let frequency = value
    frequency == 0 ? frequency = "每天" : frequency = `隔${value}天吃`
    this.setData({
      frequency,
      drugSpace:value,
      showFrequencyPick:false
    })
  },
  frequencyPickCancel(){
    this.setData({showFrequencyPick:false})
  },
  // 单位
  showUnitPick(){
    this.setData({showUnitPick:true})
  },
  unitPickConfirm(event){
    const { value, index } = event.detail
    this.setData({
      unit:value,
      showUnitPick:false
    })
  },
  unitPickCancel(){
    this.setData({showUnitPick:false})
  }
})
